#!/bin/sh

# 03.14.2006 Volker Quetschke
# Verifies that $(shell ..) does not pick up stdout output from
# concurrent processes. (Stronger test for the spawn case.)
# (issue 61856 / issue 61940)

: ${DMAKEPROG:=dmake}
file1="makefile.mk"
file2="stdout.txt"
tmpfiles="$file1 $file2"

trap '{ echo "trapped signal - removing temporary files" ; rm -rf $tmpfiles ; }' 1 2 3 15

trap 'rm -rf $tmpfiles' 1 2 3 15 

# Remember to quote variables in generated makefiles( $ -> \$ ).
cat > $file1 <<EOT
# Testing continuation char in recipes
SHELL*:=/bin/sh 
SHELLFLAGS*:=-ce

all : all1 all2
	@+echo "Y\$(DYNMAC)Y" > $file2 

all1 :
	@+echo "Before the sleeps ..."
	@+echo A; sleep 1; echo B; sleep 1; echo C; sleep 1; echo D; sleep 1; echo E
	@+echo "After the sleeps ..."

all2 :
	@+sleep 1;
	@+echo "X\$(assign DYNMAC:=\$(shell @+echo "(shellpre)";sleep 2; echo "(shellpost)"))X"
	@+echo "Z\$(DYNMAC)Z"
EOT

${DMAKEPROG} -P3 -r -f $file1
result=$?
if test "`cat $file2`" != "Y(shellpre) (shellpost)Y"; then
  echo "Wrong result: `cat $file2`, Y(shellpre) (shellpost)Y expected!"
  result=1
fi

test $result -eq 0 && echo "Success - Cleaning up" && rm -f ${tmpfiles}
test $result -ne 0 && echo "Failure!"
exit $result
